﻿using System;
using RpcClient;
using RpcHelper;
using RpcManageClient;
using RpcModel;
using SqlExecHelper;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{
        internal class VerConfigCollect : BasicCollect<ServiceVerConfigDAL>, IVerConfigCollect
        {
                public long Add (VerConfigDatum add)
                {
                        long id = this.BasicDAL.Add (add);
                        this._Refresh (add);
                        return id;
                }

                public ServiceVerConfig Get (long id)
                {
                        ServiceVerConfig config = base.BasicDAL.Get (id);
                        if (config == null)
                        {
                                throw new ErrorException ("rpc.ver.config.not.find");
                        }
                        return config;
                }
                public BasicServiceVer[] Query (VerQueryParam query, IBasicPage paging, out long count)
                {
                        return base.BasicDAL.Query (query, paging, out count);
                }
                public void Delete (ServiceVerConfig config)
                {
                        ServiceVerRouteDAL routeDAL = new ServiceVerRouteDAL ();
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                base.BasicDAL.Delete (config.Id);
                                routeDAL.Clear (config.Id);
                                tran.Commit ();
                        }
                        this._Refresh (config);
                }
                private void _Refresh (VerConfigDatum ver)
                {
                        IRpcServerCollect rpc = base.GetCollect<IRpcServerCollect> ();
                        rpc.RefreshVerConfig (ver.RpcMerId, ver.SystemTypeId, ver.VerNum);
                }
                public void Set (ServiceVerConfig ver, ServiceVerSet set)
                {
                        if (set.IsEquals (ver))
                        {
                                return;
                        }
                        base.BasicDAL.Set (ver.Id, set);
                        if (ver.IsEnable != set.IsEnable)
                        {
                                this._Refresh (ver);
                        }
                }
                public void CheckIsRepeat (long rpcMerId, long sysTypeId, long verNum)
                {
                        if (base.BasicDAL.CheckIsRepeat (rpcMerId, sysTypeId, verNum))
                        {
                                throw new ErrorException ("rpc.ver.config.repeat");
                        }
                }
                public void SetIsEnable (ServiceVerConfig config, bool isEnable)
                {
                        if (config.IsEnable == isEnable)
                        {
                                return;
                        }
                        base.BasicDAL.SetIsEnable (config.Id, isEnable);
                }

                public long Copy (ServiceVerConfig config, long ver)
                {
                        VerConfigDatum add = config.ConvertMap<ServiceVerConfig, VerConfigDatum> ();
                        add.VerNum = ver;
                        add.VerTitle = string.Concat (add.VerTitle, "-副本");
                        ServiceVerRouteDAL routeDAL = new ServiceVerRouteDAL ();
                        ServiceVerRoute[] routes = routeDAL.GetRoutes (config.Id);
                        if (routes.Length == 0)
                        {
                                return this.Add (add);
                        }
                        using (ITransaction tran = this.GetCollect<ITransaction> ())
                        {
                                long id = this.BasicDAL.Add (add);
                                routeDAL.Adds (routes, id);
                                tran.Commit ();
                                return id;
                        }

                }
        }
}
